# -*- coding: utf-8 -*-

from flask import request, session, redirect, url_for, abort, jsonify
from wechatpy.oauth import WeChatOAuth

from manage import application as app
from mpdev.wechat_token import APPID, APPSECRET, TOKEN

OAUTH_SCOPE = 'snsapi_userinfo'
OAUTH_STATE = '1'
# non-sense
OAUTH_REDIRECT_URL = 'www.guanjia.group/oauth2'

USER_INFO_KEY = ['openid', 'nickname', 'headimgurl']


@app.route('/oauth2/gzone/<enc_room_id>', endpoint='gzone_oauth2')
def gzone_oauth2(enc_room_id):
    code = request.args.get('code')
    if code is None:
        # todo: 当code不存在时的错误页面
        abort(400)
    ts = request.args.get('ts')
    return redirect(url_for('gzone.index', enc_room_id=enc_room_id, ts=ts, code=code))


@app.route('/welcome', methods=['GET', 'POST'])
def welcome():
    info = dict(
        message=u'这是一个测试字符串'
    )
    import os
    from flask import render_template
    from flask_wtf import FlaskForm
    from flask_wtf.file import FileField, FileAllowed
    from wtforms import StringField, PasswordField, SubmitField
    from wtforms.validators import DataRequired, EqualTo
    class AnnouncementForm(FlaskForm):
        title = StringField(u'标题', validators=[DataRequired(u'请输入标题')])
        content = StringField(u'内容', validators=[DataRequired(u'请输入内容')])
        image = FileField(
            u'附图',
            validators=[FileAllowed(['jpg', 'png'], u'不支持图片之外类型')]
        )
        submit = SubmitField(u'添加')
    sticky = request.args.get('sticky')
    if sticky is not None:
        print 'hhh'
        print sticky
    else:
        print 'lll'
    form = AnnouncementForm()
    if form.validate_on_submit():
        img = form.image.data
        img.save(os.path.join(
            app.instance_path,
            app.static_folder,
            img.filename
        ))
        return 'hhh'
    return render_template('welcome.html', form=form)
    # return jsonify(info)
